package com.liu.blindBox.service.impl;

import com.liu.blindBox.entity.base.SaleInfoBase;
import com.liu.blindBox.entity.dto.BlindBoxAmountInfoDTO;
import com.liu.blindBox.entity.dto.UserDeductionInfoDTO;
import com.liu.blindBox.entity.qo.SaleInfoQO;
import com.liu.blindBox.mapper.BuyBlindBoxMapper;
import com.liu.blindBox.service.BuyBlindBoxService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BuyBlindBoxServiceImpl implements BuyBlindBoxService {

    @Resource
    private BuyBlindBoxMapper buyBlindBoxMapper;

    @Override
    public int buyBlindBox(List<SaleInfoQO> saleInfoQOList, String userId) {
        List<SaleInfoBase> saleInfoBaseList = new ArrayList<>();
        List<UserDeductionInfoDTO> userDeductionInfoDTOList = new ArrayList<>();
        List<String> blindBoxIdList = saleInfoQOList.stream().map(SaleInfoQO::getBlindBoxId).collect(Collectors.toList());
        // 获取盲盒的金额信息
        List<BlindBoxAmountInfoDTO> blindBoxAmountInfoDTOS = buyBlindBoxMapper.queryBlindBoxAmountInfo(blindBoxIdList);
        // 获取用户余额
        BigDecimal userBalance = buyBlindBoxMapper.queryUserBalance(userId);
        Map<String, BlindBoxAmountInfoDTO> blindBoxAmountInfoDTOMap = blindBoxAmountInfoDTOS.stream().collect(Collectors.toMap(BlindBoxAmountInfoDTO::getBlindBoxId, b -> b));
        // 总共需要支付的金额
        BigDecimal totalPayment = BigDecimal.ZERO;
        // 总成本
        BigDecimal totalCost = BigDecimal.ZERO;
        // 总利润
        BigDecimal totalProfit = BigDecimal.ZERO;
        for (SaleInfoQO saleInfoQO : saleInfoQOList) {
            SaleInfoBase saleInfoBase = new SaleInfoBase();
            BlindBoxAmountInfoDTO blindBoxAmountInfoDTO = blindBoxAmountInfoDTOMap.get(saleInfoQO.getBlindBoxId());
            totalPayment = totalPayment.add(blindBoxAmountInfoDTO.getBlindBoxPrice().multiply(BigDecimal.valueOf(saleInfoQO.getQuantity())));
            totalCost = totalCost.add(blindBoxAmountInfoDTO.getBlindBoxAverageCost().multiply(BigDecimal.valueOf(saleInfoQO.getQuantity())));
            totalProfit = totalProfit.add(blindBoxAmountInfoDTO.getBlindBoxAverageProfit().multiply(BigDecimal.valueOf(saleInfoQO.getQuantity())));
            saleInfoBase.setBlindBoxId(saleInfoQO.getBlindBoxId());
            saleInfoBase.setBlindBoxSaleNum(saleInfoQO.getQuantity());
            saleInfoBase.setBlindBoxSaleAmount(totalPayment);
            saleInfoBase.setBlindBoxTotalProfit(totalProfit);
            saleInfoBase.setBlindBoxTotalCost(totalCost);
            saleInfoBaseList.add(saleInfoBase);
            UserDeductionInfoDTO userDeductionInfoDTO = new UserDeductionInfoDTO();
            userDeductionInfoDTO.setUserId(userId);
            userDeductionInfoDTO.setDeductionAmountOfMoney(totalPayment);
            userDeductionInfoDTO.setDeductionInfo("购买【"+blindBoxAmountInfoDTO.getBlindBoxName()+"】x【"+saleInfoQO.getQuantity()+"】花费【"+blindBoxAmountInfoDTO.getBlindBoxPrice()+"￥】x【"+saleInfoQO.getQuantity()+"】=【"+totalPayment+"￥】");
            userDeductionInfoDTOList.add(userDeductionInfoDTO);
        }
        if (userBalance.compareTo(totalPayment) >= 0){
            // 更新用户余额
            buyBlindBoxMapper.updateUserBalance(userId, userBalance.subtract(totalPayment));
            // 将销售信息插入数据库
            if (!CollectionUtils.isEmpty(saleInfoBaseList)){
                buyBlindBoxMapper.insertBlindBoxAmountInfo(saleInfoBaseList);
            }
            // 更新用户消费记录表
            if (!CollectionUtils.isEmpty(userDeductionInfoDTOList)){
                buyBlindBoxMapper.insertUserDeduction(userDeductionInfoDTOList);
            }
            return 1;
        }else {
            return 0;
        }
    }
}
